#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   db_models.py
@Time    :   2023/04/03 21:57:33
@Author  :   youle
@Email   :   a_fly0505@163.com
@Copyright : 侵权必究
'''

from core.config import Config
from sqlalchemy import Column, Date, Float, Integer, text, Boolean, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()
Metadata = Base.metadata
Engine = None


def Init(config: Config) -> None:
    db_sql = "postgresql://{}:{}@{}:{}/{}".format(
        config.user, config.password, config.host, config.port, config.database)
    global Engine
    Engine = create_engine(db_sql, echo=False)

# 股票
class MarketStock(Base):
    __tablename__ = 'stock'
    __table_args__ = {'schema': 'stock_market'}

    code = Column(String(32), primary_key=True)
    sname = Column(String(128), nullable=False)
    exchange = Column(String(128))
    ah = Column(Boolean)
    entry_time = Column(Date)

# ETF
class MarketFunc(Base):
    __tablename__ = 'fund'
    __table_args__ = {'schema': 'stock_market'}

    code = Column(String(32), primary_key=True)
    sname = Column(String(128), nullable=False)
    exchange = Column(String(128))
    ah = Column(Boolean)
    entry_time = Column(Date)

# 单只股票
class StockQfq(Base):
    __tablename__ = 'stock_qfq_default'
    __table_args__ = {'schema': 'stock_datas'}

    id = Column(Integer, primary_key=True, autoincrement=True)
    date = Column(Date, nullable=False, unique=True)
    open = Column(Float, nullable=False)
    close = Column(Float, nullable=False)
    hight = Column(Float, nullable=False)
    low = Column(Float, nullable=False)
    volume = Column(Float(53), server_default=text("0.0"))
    volume_quota = Column(Float(53), server_default=text("0.0"))
    vibration = Column(Float)
    rise_fall_rate = Column(Float, server_default=text("0.0"))
    rise_fall_quota = Column(Float, server_default=text("0.0"))
    turnover_rate = Column(Float)

# 单只ETF
class FundQfq(Base):
    __tablename__ = 'fund_default'
    __table_args__ = {'schema': 'fund'}

    id = Column(Integer, primary_key=True, autoincrement=True)
    date = Column(Date, nullable=False, unique=True)
    open = Column(Float, nullable=False)
    close = Column(Float, nullable=False)
    hight = Column(Float, nullable=False)
    low = Column(Float, nullable=False)
    volume = Column(Float(53), server_default=text("0.0"))
    volume_quota = Column(Float(53), server_default=text("0.0"))
    vibration = Column(Float)
    rise_fall_rate = Column(Float, server_default=text("0.0"))
    rise_fall_quota = Column(Float, server_default=text("0.0"))
    turnover_rate = Column(Float)


def make_fund(name):
    class FundObject(Base):
        __tablename__ = name
        __table_args__ = {'schema': 'fund'}

        id = Column(Integer, primary_key=True, autoincrement=True)
        date = Column(Date, nullable=False, unique=True)
        open = Column(Float, nullable=False)
        close = Column(Float, nullable=False)
        hight = Column(Float, nullable=False)
        low = Column(Float, nullable=False)
        volume = Column(Float(53), server_default=text("0.0"))
        volume_quota = Column(Float(53), server_default=text("0.0"))
        vibration = Column(Float)
        rise_fall_rate = Column(Float, server_default=text("0.0"))
        rise_fall_quota = Column(Float, server_default=text("0.0"))
        turnover_rate = Column(Float)

        def __init__(self, date, open, close, hight, low, volume, volume_quota, vibration, rise_fall_rate, rise_fall_quota, turnover_rate):
            self.date = date
            self.open = open
            self.close = close
            self.hight = hight
            self.low = low
            self.volume = volume
            self.volume_quota = volume_quota
            self.vibration = vibration
            self.rise_fall_rate = rise_fall_rate
            self.rise_fall_quota = rise_fall_quota
            self.turnover_rate = turnover_rate

    return FundObject
